package com.mjf.jst.util;

import cn.hutool.core.util.StrUtil;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @Author zzz
 */
public class DateAndTimeUtils {

    private DateAndTimeUtils() {
        throw new IllegalStateException("Utility class");
    }

    /**
     * 将LocalDateTime转为自定义的时间格式的字符串
     *
     * @param localDateTime localDateTime
     * @param format        format
     * @return String
     */
    public static String getStringByDateTime(LocalDateTime localDateTime, String format) {
        if (localDateTime == null) {
            return null;
        }
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
        return localDateTime.format(formatter);
    }

    /**
     * 将自定义的时间格式的字符串转为LocalDateTime
     *
     * @param localDateTime localDateTime
     * @param format        format
     * @return String
     */
    public static LocalDateTime getDateTimeByStr(String localDateTime, String format) {
        if (StrUtil.isBlank(localDateTime)) {
            return null;
        }
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
        return LocalDateTime.parse(localDateTime, formatter);
    }

}
